package com.eff.android;

import com.eff.android.api.model.Setting;
import com.eff.android.fragment.EFragmentFactory;
import com.eff.android.fragment.main.EMainFragment;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;

public class EMainActivity extends EBaseActivity{

	 	public static String TAG = EMainActivity.class.getSimpleName();

	    private FragmentManager mFragmentManager;
	    private FragmentTransaction mFragmentTransaction;
	    private String mCurrentFragmentTag;
	    
	    private Setting settings;
	    
	    @Override
	    public void onCreate(Bundle savedInstanceState) {
			super.onCreate(savedInstanceState);
			setContentView(R.layout.e_activity_main);
	
			mFragmentManager = getSupportFragmentManager();
			settings = Setting.newInstance();
	
			Bundle bundle = new Bundle();
			bundle.putSerializable(Setting.TAG, settings);
			initFragment(bundle);
			
			getActionBar().hide();
	    }

	    private void initFragment(Bundle bundle) {
	    	EFragmentFactory factory = EFragmentFactory.newInstance();
	    	EMainFragment mainFrm = (EMainFragment) factory.CreateFragment(EMainFragment.TAG, bundle);
	    	setupContentViewWithNoBackStack(mainFrm, EMainFragment.TAG);
	    }

	    // ---------------------------------------------------------------------------------

	    public void setupContentViewWithNoBackStack(Fragment fragment, String fragmentTag){
			setupContentView(fragment, fragmentTag, false);
	    }

	    private void setupContentView(Fragment fragment, String fragmentTag,
		    boolean isAddToBackStack) {
			if (mCurrentFragmentTag != null
				&& !mCurrentFragmentTag.equalsIgnoreCase(fragmentTag))
			    detachFragment(getFragmentByTag(mCurrentFragmentTag), false);
	
			setupContentFragment(fragment, mCurrentFragmentTag,
					isAddToBackStack);
	    }

	    // ---------------------------------------------------------------------------------
	    // CONTENT FRAGMENT REGION
	    public void setupContentFragment(Fragment fragment, String tagName,
		    boolean isAddToBackStack) {
	    	attachFragment(R.id.content_frame, fragment, tagName,
					isAddToBackStack, true);
			commitTransactions();
	
	    }

	    private void attachFragment(int layout, Fragment f, String tag,
		    boolean isAddToBackStack, boolean shouldAnimate) {
			mFragmentManager.executePendingTransactions();
			if (f != null) {
			    if (f.isDetached()) {
					ensureTransaction(shouldAnimate);
					mFragmentTransaction.attach(f);
					if (isAddToBackStack)
					    mFragmentTransaction.addToBackStack(tag);
			    } else {
					ensureTransaction(shouldAnimate);
					mFragmentTransaction.replace(layout, f, tag);
					if (isAddToBackStack)
					    mFragmentTransaction.addToBackStack(tag);
			    }
			}
	    }

	    private FragmentTransaction ensureTransaction(boolean shouldAnimate) {
			if (mFragmentTransaction == null) {
			    mFragmentTransaction = mFragmentManager.beginTransaction();
	
			    if (shouldAnimate) {
					mFragmentTransaction.setCustomAnimations(android.R.anim.slide_in_left, 0, 
							android.R.anim.fade_out, 0);
			    } else {
			    	mFragmentTransaction.setCustomAnimations(0, 0, 0, 0);
			    }
			}
			
			return mFragmentTransaction;
	    }

	    public Fragment getFragmentByTag(String tag) {
			Fragment f = mFragmentManager.findFragmentByTag(tag);
			return f;
	    }

	    public void clearAllFragmentsInStack() {
		mFragmentManager.popBackStack(null,
			FragmentManager.POP_BACK_STACK_INCLUSIVE);
	    }

	    private void detachFragment(Fragment f, boolean shouldAnimate) {
			if (f != null && !f.isDetached()) {
			    ensureTransaction(shouldAnimate);
			    mFragmentTransaction.detach(f);
			}
	    }

	    private void commitTransactions() {
			if (mFragmentTransaction != null && !mFragmentTransaction.isEmpty()) {
			    mFragmentTransaction.commitAllowingStateLoss();
			    mFragmentTransaction = null;
			}
	    }
}
